package cn.tedu.mintmall.service.Impl;

import cn.tedu.mintmall.dto.ProductDetailsDTO;
import cn.tedu.mintmall.entity.ProductDetails;
import cn.tedu.mintmall.exception.MintmallServiceException;
import cn.tedu.mintmall.mapper.ProductDetailsMapper;
import cn.tedu.mintmall.restful.JsonResult;
import cn.tedu.mintmall.restful.ResponseCode;
import cn.tedu.mintmall.service.IProductDetails;
import cn.tedu.mintmall.vo.ProductDetailsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.image.ImageProducer;
@Service
public class ProductDetailsImpl implements IProductDetails {
    @Autowired
    private ProductDetailsMapper mapper;
    @Override
    public void addProductDetails(ProductDetailsDTO productDetailsDTO) {
        //创建一个实体类  添加商品详情的方法参数为ProductDetails
        ProductDetails productDetails=new ProductDetails();
        //赋值同名属性
        BeanUtils.copyProperties(productDetailsDTO,productDetails);
        mapper.insertProductDetails(productDetails);
    }

    //根据商品Id查询商品详情
    @Override
    public ProductDetailsVO queryDetails(Integer productId) {
        ProductDetails productDetails = mapper.selectByProductId(productId);
        ProductDetailsVO productDetailsVO=new ProductDetailsVO();
        BeanUtils.copyProperties(productDetails,productDetailsVO);
        return productDetailsVO;
    }

    //
    @Override
    public void updateDetails(ProductDetailsDTO productDetailsDTO) {
        int row = mapper.updateProductDetails(productDetailsDTO);
        if (row==0){
            throw new MintmallServiceException(ResponseCode.BAD_REQUEST,"请输入正确的商品Id");
        }
    }

    @Override
    public void deleteDetails(Integer productId) {
        int rows = mapper.deleteProductDetails(productId);
        if (rows==0){
            throw new MintmallServiceException(ResponseCode.BAD_REQUEST,"请输入正确的商品Id");
        }
    }
}
